﻿using ClipperLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.DEMO.Pages
{
    /// <summary>
    /// ClipperTestPage.xaml 的交互逻辑
    /// </summary>
    public partial class ClipperTestPage : Page
    {
        public ClipperTestPage()
        {
            InitializeComponent();
            this.Loaded += ClipperTestPage_Loaded;
        }

        WriteableBitmap bmp;
        private void ClipperTestPage_Loaded(object sender, RoutedEventArgs e)
        {
            bmp = new WriteableBitmap(1000, 600, 96, 96, PixelFormats.Pbgra32, BitmapPalettes.WebPaletteTransparent);
            img.Source = bmp;


        }

        ClipperLib.ClipperOffset co = new ClipperLib.ClipperOffset();
        List<IntPoint> ps = new List<IntPoint>();
        private Random random = new Random();
        private void btn_addpoint_Click(object sender, RoutedEventArgs e)
        {
            //    for (int i = 0; i < 4; i++)
            //    {
            //        ps.Add(new IntPoint(random.Next(100, 1000), random.Next(100, 1000)));
            //    }

            ps.Add(new IntPoint(200, 100));
            ps.Add(new IntPoint(250, 121));
            ps.Add(new IntPoint(212, 202)); 
            co.AddPath(ps, JoinType.jtRound, EndType.etOpenButt);
         
            List<List<IntPoint>> solution2 = new List<List<IntPoint>>();
            co.Execute(ref solution2, 10);




            foreach (var sol in solution2)
            {
                var edge = sol;
                var color = WriteableBitmapExtensions.ConvertColor(Colors.Red);
                List<int> edgePoints = new List<int>();
                foreach (var item in edge)
                {
                    edgePoints.Add((int)item.X);
                    edgePoints.Add((int)item.Y);

                }
                edgePoints.Add(edgePoints[0]);
                edgePoints.Add(edgePoints[1]);
                bmp.FillPolygon(edgePoints.ToArray(), Colors.Red);
            }


            List<int> Points = new List<int>();
            foreach (var item in ps)
            {
                Points.Add((int)item.X);
                Points.Add((int)item.Y);

            } 
            bmp.DrawPolyline(Points.ToArray(), Colors.Blue);


        }

        private void btn_clear_Click(object sender, RoutedEventArgs e)
        {
            ps.Clear();
            co.Clear();
            bmp.Clear(Colors.White);
        }
    }
}
